package com.xywang.filter;

import com.xywang.pojo.User;
import com.xywang.util.Constants;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class FilterSys implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) servletRequest;
        HttpServletResponse resp = (HttpServletResponse) servletResponse;
        // 过滤器，从Session中获取用户对象，注销（或者 设置了半小时session自动消失）
        // 后session消失，user对象也消失，则会进入错误页面
        User user = (User) req.getSession().getAttribute(Constants.USER_SESSION);

        if (user == null) {
            resp.sendRedirect(req.getContextPath()+"/error.jsp");
        }else {
            filterChain.doFilter(servletRequest,servletResponse);
        }


    }

    @Override
    public void destroy() {

    }
}
